package com.syni.mdd.yhd.common.all.quartz.groupMessage;

import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessGroupMessage;
import com.syni.mdd.yhd.common.all.service.BmsBusinessGroupMessageService;
import com.syni.mdd.yhd.common.all.service.BmsBusinessService;
import com.syni.mdd.sjb.common.component.utils.common.utils.SpringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.*;

/**
 * @className GroupMessageQuartz
 * @description TOO
 * @Author cfx
 * @DATE 2019/7/8 9:37
 * @VERSION 1.0
 **/
public class GroupMessageTimingJob implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        BmsBusinessGroupMessageService bmsBusinessGroupMessageService = (BmsBusinessGroupMessageService) SpringUtils.getBean("bmsBusinessGroupMessageService");
        BmsBusinessService bmsBusinessService = (BmsBusinessService) SpringUtils.getBean("bmsBusinessService");
        Map<String,Object> paramsMap = jobExecutionContext.getJobDetail().getJobDataMap();
        BmsBusinessGroupMessage bmsBusinessGroupMessage = (BmsBusinessGroupMessage) paramsMap.get("bmsBusinessGroupMessage");
        List<String> sendUsers = (List<String>) paramsMap.get("sendUsers");
        bmsBusinessGroupMessageService.doSendUserOpByTiming(bmsBusinessGroupMessage);
        //往消息表发送消息
        Set<Integer> userIds = new HashSet<>();
        for(String sendUser : sendUsers){
            userIds.add(Integer.parseInt(sendUser));
        }
        Optional<BmsBusiness> bmsBusinessOptional = bmsBusinessService.getBaseDao().findById(bmsBusinessGroupMessage.getBmsBusinessId());
        if(bmsBusinessOptional.isPresent()) {
            bmsBusinessGroupMessageService.addGroupMessageToNews(bmsBusinessOptional.get(),
                    userIds, bmsBusinessGroupMessage);
        }
    }
}
